import protoRoot from "@/net/proto/proto";
import {ToastTime} from "@/const/Const";


/**
 * Created with JackHou
 * Date: 2019/3/27
 * Time: 16:25
 * Description:API 2.x Ws注解
 */
const Code = protoRoot.lookupEnum("Code");

function WsHandler(...definesKey) {
    return (target, name, descriptor) => {
        // console.log('definesKey', ...definesKey);
        // console.log('target:', target, '\n name:', name, '\n descriptor:', descriptor.value);
        let original = descriptor.value;
        console.log(original)
        descriptor.value = async (...params) => {
            const queryParams = {};
            if (definesKey.length < params.length) {
                throw new Error('配对参数不一致');
            }
            for (let i = 0; i < definesKey.length; i++) {
                if (typeof definesKey[i] === 'object') {
                    Object.assign(queryParams, definesKey[i]);
                } else {
                    queryParams[definesKey[i]] = params[i];
                }
            }
            return await WsPublicHandler(queryParams);
        };
        return descriptor;
    }
}


const WsPublicHandler = (params) => {
    // console.log('params', params.response, params.success, !params.response, params.response.code === Code.Success);
    if (params.response && params.response.code === Code.values.Success) {
        console.log(params.success);
    } else if (params.response && params.response.code !== Code.values.Success && params.failed) {
        console.error(params.failed);
    }
};

export {
    WsHandler
}